﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SocialPlatforms;


using Script.Data;
using System.Security.Cryptography.X509Certificates;
using MTUnity.Utils;

public class FWarehouse
{
    private static FWarehouse s_Warehouse;
    public static FWarehouse GetInstance()
    {
        
        if (s_Warehouse == null)
        {
            s_Warehouse = new FWarehouse();
            s_Warehouse.LoadConfig();
        }
        return s_Warehouse;
    }

    private void LoadConfig()
    {
        foreach (var item in PositionConf.PositionConfDict)
        {
            FLocation location = AddLocation(int.Parse(item.Key), item.Value.notecn, item.Value.noteen, item.Value.code, item.Value.posType, int.Parse(item.Value.previewId));
        }
    }
    
    public string Name { get; set; }
    

    private List<int> m_listAllLocationID = new List<int>();
    private Dictionary<int, FLocation> m_dicLocation = new Dictionary<int, FLocation>();

    public void SetCurrentTemperature(ELocationType eLocationType , double dCurrentTemperature)
    {
        FLocation location = GetLocationWithID(eLocationType == ELocationType.E_Enter ? 2 : 3);
        FEnterOrExitData enterOrExitData = (FEnterOrExitData)location.Data;
        enterOrExitData.CurrentTemperature = dCurrentTemperature;

    }

    public void SetCurrentHumidity(ELocationType eLocationType, double dCurrentHumidity)
    {
        FLocation location = GetLocationWithID(eLocationType == ELocationType.E_Enter ? 2 : 3);
        FEnterOrExitData enterOrExitData = (FEnterOrExitData)location.Data;
        enterOrExitData.CurrentHumidity = dCurrentHumidity;
    }

    public void SetCurrentStockInOrOut(ELocationType eLocationType, int iStockInOrOut , double dEfficient)
    {
        FLocation location = GetLocationWithID(eLocationType == ELocationType.E_Enter ? 2 : 3);
        FEnterOrExitData enterOrExitData = (FEnterOrExitData)location.Data;
        enterOrExitData.CurrentInOrOutStock = iStockInOrOut;
        enterOrExitData.CurrentInOrOutStockEfficient = dEfficient;
    }



    public double GetCurrentTemperature(ELocationType eLocationType)
    {
        FLocation location = GetLocationWithID(eLocationType == ELocationType.E_Enter ? 2 : 3);
        FEnterOrExitData enterOrExitData = (FEnterOrExitData)location.Data;
        return enterOrExitData.CurrentTemperature;

    }

    public double GetCurrentHumidity(ELocationType eLocationType)
    {
        FLocation location = GetLocationWithID(eLocationType == ELocationType.E_Enter ? 2 : 3);
        FEnterOrExitData enterOrExitData = (FEnterOrExitData)location.Data;
        return enterOrExitData.CurrentHumidity;

    }

    public void GetCurrentStockInOrOut(ELocationType eLocationType , out int iStockInOrOut , out double dEfficient)
    {
        FLocation location = GetLocationWithID(eLocationType == ELocationType.E_Enter ? 2 : 3);
        FEnterOrExitData enterOrExitData = (FEnterOrExitData)location.Data;

        iStockInOrOut = enterOrExitData.CurrentInOrOutStock;
        dEfficient = enterOrExitData.CurrentInOrOutStockEfficient;
    }

    public List<FWarehouseHumitureRecord> GetNearestHumiture(ELocationType eLocationType, int iCount = 1)
    {
        FLocation location = GetLocationWithID(eLocationType == ELocationType.E_Enter ? 2 : 3);
        FEnterOrExitData enterOrExitData = (FEnterOrExitData)location.Data;

        return enterOrExitData.GetNearestHumiture(iCount);

    }

    public void AddHumitureRecord(ELocationType eLocationType, double dTemperature , double dHumidity , long lTimestamp)
    {
        FLocation location = GetLocationWithID(eLocationType == ELocationType.E_Enter ? 2 : 3);
        FEnterOrExitData enterOrExitData = (FEnterOrExitData)location.Data;
        enterOrExitData.AddHumitureRecord(dTemperature, dHumidity, lTimestamp);

    }

    public void AddRepertoryRecord(ELocationType eLocationType , int iRepertoryInBuff , int iRepertoryInStore , long lTimestamp)
    {
        FLocation location = GetLocationWithID(eLocationType == ELocationType.E_Enter ? 2 : 3);
        FEnterOrExitData enterOrExitData = (FEnterOrExitData)location.Data;
        enterOrExitData.AddRepertoryRecord(iRepertoryInBuff, iRepertoryInStore, lTimestamp);

    }
     
    public List<FWarehouseRepertoryRecord> GetNearestRepertoryRecord(ELocationType eLocationType , int iCount)
    {
        FLocation location = GetLocationWithID(eLocationType == ELocationType.E_Enter ? 2 : 3);
        FEnterOrExitData enterOrExitData = (FEnterOrExitData)location.Data;

        return enterOrExitData.GetNearestRepertoryRecord(iCount);

    }

    public void AddStockInOrOutRecord(ELocationType eLocationType , int iAverage, double Efficient, long lTimestamp)
    {
        FLocation location = GetLocationWithID(eLocationType == ELocationType.E_Enter ? 2 : 3);
        FEnterOrExitData enterOrExitData = (FEnterOrExitData)location.Data;
        enterOrExitData.AddStockInOrOutRecord(iAverage, Efficient, lTimestamp);
    }

    public List<FWarehouseStockInOrOutRecord> GetNearestStockInOrOutRecord(ELocationType eLocationType , int iCount)
    {
        FLocation location = GetLocationWithID(eLocationType == ELocationType.E_Enter ? 2 : 3);
        FEnterOrExitData enterOrExitData = (FEnterOrExitData)location.Data;

        return enterOrExitData.GetNearestStockInOrOutRecord(iCount);
    }

    public void SetCurrentRepertory(ELocationType eLocationType ,  int iCurrentRepertoryInBuff , int iCurrentRepertoryInStore)
    {
        FLocation location = GetLocationWithID(eLocationType == ELocationType.E_Enter ? 2 : 3);
        FEnterOrExitData enterOrExitData = (FEnterOrExitData)location.Data;

        enterOrExitData.CurrentRepertoryInBuff = iCurrentRepertoryInBuff;
        enterOrExitData.CurrentRepertoryInStore = iCurrentRepertoryInStore;
        
    }

    public void GetCurrentRepertory(ELocationType eLocationType, out int iCurrentRepertoryInBuff, out int iCurrentRepertoryInStore)
    {
        FLocation location = GetLocationWithID(eLocationType == ELocationType.E_Enter ? 2 : 3);
        FEnterOrExitData enterOrExitData = (FEnterOrExitData)location.Data;
        iCurrentRepertoryInBuff = enterOrExitData.CurrentRepertoryInBuff;
        iCurrentRepertoryInStore = enterOrExitData.CurrentRepertoryInStore;
    }

    #region Sorting Line
    public List<int> GetAllSortingLineID()
    {
        List<int> listResult = new List<int>();
        foreach(var item in m_dicLocation)
        {
            if (ToolUtils.LocationCheck(item.Value.LocationType , ELocationType.E_SortingLine))
            {
                listResult.Add(item.Key);
            }
            
        }

        listResult.Sort(
            (int a, int b) => 
            {
                if (a > b)
                {
                    return 1;
                }
                else if (a < b)
                {
                    return -1;
                }
                else
                {
                    return 0;
                }
            }
            );
        return listResult;
    }
    public FSortingLine GetSortingLineWithID(int iID)
    {
        FLocation location = GetLocationWithID(iID);
        if (ToolUtils.LocationCheck(location.LocationType, ELocationType.E_SortingLine))
        {
            return (FSortingLine)location.Data;
        }
        else
        {
            return null;
        }
    }
    

    public double GetCurrentEnergyConsumption(int iID)
    {
        FSortingLine sortingLine = GetSortingLineWithID(iID);
        return sortingLine.CurrentEnergyConsumption;
    }

    public void GetCurrentSortingCount(int iID , out int iSortingCountInPlan , out int iSourtCountInCompleted)
    {
        FSortingLine sortingLine = GetSortingLineWithID(iID);
        iSortingCountInPlan = sortingLine.CurrentSortingData.SortingCountInPlan;
        iSourtCountInCompleted = sortingLine.CurrentSortingData.SortingCountInCompleted;
    }

    public double GetCurrentSortingDuration(int iID)
    {
        FSortingLine sortingLine = GetSortingLineWithID(iID);
        return sortingLine.CurrentSortingData.SortingDuration;
    }

    public double GetCurrentSortingEfficient(int iID)
    {
        FSortingLine sortingLine = GetSortingLineWithID(iID);
        return sortingLine.CurrentSortingData.SortingEfficient;
    }

    public void GetCurrentSortingCheck(int iID , out int iSortingCheckInPlan , out int iSortingCheckInCompleted)
    {
        FSortingLine sortingLine = GetSortingLineWithID(iID);
        iSortingCheckInPlan = sortingLine.CurrentSortingCheckInPlan;
        iSortingCheckInCompleted = sortingLine.CurrentSortingCheckInCompleted;
    }

    public List<FEnergyConsumptionRecord> GetNearestEnergyConsumption(int iID, int iCount)
    {

        FSortingLine sortingLine = GetSortingLineWithID(iID);

        return sortingLine.GetNearestEnergyConsumptionRecord(iCount);
    }

    public List<FSortingCountRecord> GetNearestSortingCountRecord(int iID, int iCount)
    {
        FSortingLine sortingLine = GetSortingLineWithID(iID);

        return sortingLine.GetNearestSortingCountRecord(iCount);
    }

    public List<FSortingDurationRecord> GetNearestSortingDurationRecord(int iID, int iCount)
    {
        FSortingLine sortingLine = GetSortingLineWithID(iID); ;

        return sortingLine.GetNearestSortingDurationRecord(iCount);
    }

    public List<FSortingEfficientRecord> GetNearestSortingEfficientRecord(int iID, int iCount)
    {
        FSortingLine sortingLine = GetSortingLineWithID(iID);

        return sortingLine.GetNearestSortingEfficientRecord(iCount);
    }

    public List<FSortingCheckRecord> GetNearestSortingCheckRecord(int iID, int iCount)
    {
        FSortingLine sortingLine = GetSortingLineWithID(iID);

        return sortingLine.GetNearestSortingCheckRecord(iCount);
    }
	#endregion

	#region ExitOrEnter
    public FLocation GetEnter()
    {
        return GetLocationWithType(ELocationType.E_Enter)[0];
    }

    public FLocation GetExit()
    {
        return GetLocationWithType(ELocationType.E_Exit)[0];
    }
    #endregion

    public FLocation GetLocationWithID(int iID)
    {
        if (m_dicLocation.ContainsKey(iID))
        {
            return m_dicLocation[iID];
        }
        return null;
    }

    public List<int> GetAllLocationID()
    {
        return m_listAllLocationID;
    }

    public List<FLocation> GetLocationWithType(ELocationType eLocationType)
    {
        List<int> listResult = new List<int>();
        foreach (var item in m_dicLocation)
        {
            if (ToolUtils.LocationCheck(item.Value.LocationType, eLocationType))
            {
                listResult.Add(item.Key);
            }
        }

        listResult.Sort(
            (int a, int b) =>
            {
                if (a > b)
                {
                    return 1;
                }
                else if (a < b)
                {
                    return -1;
                }
                else
                {
                    return 0;
                }
            }
            );

        List<FLocation> listResultLocation = new List<FLocation>();
        foreach(var item in listResult)
        {
            listResultLocation.Add(m_dicLocation[item]);
        }
        return listResultLocation;
    }

    public T GetLocationDataWithID<T>(int iID) where T : DataBase
    {
        FLocation location = GetLocationWithID(iID);
        return location.Data as T;
    }
    public FLocation AddLocation(int iID , string strCNName, string strENName, string strCode , int iType , int iGroup)
    {
        if (m_dicLocation.ContainsKey(iID))
        {
            return null;
        }
        FLocation location = new FLocation(iID);
        location.CNName = strCNName;
        location.ENName = strENName;
        location.Code = strCode;
        location.GroupID = iGroup;
        location.SetLocationType((ELocationType)iType);
        m_listAllLocationID.Add(iID);
        m_dicLocation.Add(iID, location);

        return location;
    }

    public void SetTime(ELocationType eLocationType, long time)
    {
        FLocation location = GetLocationWithID(eLocationType == ELocationType.E_Enter ? 2 : 3);
        FEnterOrExitData enterOrExitData = (FEnterOrExitData)location.Data;
        enterOrExitData.Time = time;
    }

	#region Test
	public static FWarehouse GenerateTestData()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();


        ////ware
        //UpdateCurremtTemperatureTest();
        //UpdateCurrentHumidityTest();
        //UpdateCurrentRepertoryTest();
        //UpdateCurrentStockInOrOutTest();


        ////UpdateRecordHumitureTest();
        ////UpdateRecordRepertoryTest();
        ////UpdateRecordStockInOrOutTest();

        ////sortline
        //UpdateCurrentEnergyConsumptionTest();
        //UpdateCurrentSortingLineDataTest();
        //UpdateCurrentSortingCheckTest();

        //UpdateRecordEnergyConsumptionTest();
        //UpdateRecordSortingLineDataTest();
        //UpdateRecordSortingCheckTest();

        ////overview
        //UpdateVisitorCurrentDataTest();
        //UpdateAirPressureCurrentDataTest();
        //UpdateVisitorCurrentDataTest();

        //UpdateVisitorRecordData();
        //UpdateAirPressureRecordData();
        //UpdateVoltageRecordData();

        return warehouse;
    }

	#region EnterOrExit
	public static void UpdateCurremtTemperatureTest()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();
        warehouse.SetCurrentTemperature(ELocationType.E_Enter, rm.Next(-30, 30));
        warehouse.SetCurrentTemperature(ELocationType.E_Exit, rm.Next(-30, 30));
    }

    public static void UpdateCurrentHumidityTest()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();
        warehouse.SetCurrentHumidity(ELocationType.E_Enter, rm.Next(0, 100));
        warehouse.SetCurrentHumidity(ELocationType.E_Exit, rm.Next(0, 100));
    }

    public static void UpdateRecordHumitureTest()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();

        for (int i = 0; i < 12; i++)
        {
            warehouse.AddHumitureRecord(ELocationType.E_Enter, rm.Next(-30, 30), rm.Next(0, 100), i);
            warehouse.AddHumitureRecord(ELocationType.E_Exit, rm.Next(-30, 30), rm.Next(0, 100), i);
        }
    }

    public static void UpdateCurrentRepertoryTest()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();

        warehouse.SetCurrentRepertory(ELocationType.E_Enter, rm.Next(10000), rm.Next(10000));
        warehouse.SetCurrentRepertory(ELocationType.E_Exit, rm.Next(10000), rm.Next(10000));
    }

    public static void UpdateRecordRepertoryTest()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();

        for (int i = 0; i < 12; i++)
        {
            warehouse.AddRepertoryRecord(ELocationType.E_Enter, rm.Next(0, 1000), rm.Next(0, 1000), i);
            warehouse.AddRepertoryRecord(ELocationType.E_Exit, rm.Next(0, 1000), rm.Next(0, 1000), i);
        }
    }

    public static void UpdateCurrentStockInOrOutTest()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();

        warehouse.SetCurrentStockInOrOut(ELocationType.E_Enter, rm.Next(0, 1000), rm.Next(0, 100));
        warehouse.SetCurrentStockInOrOut(ELocationType.E_Exit, rm.Next(0, 1000), rm.Next(0, 100));
    }
    public static void UpdateRecordStockInOrOutTest()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();

        for (int i = 0; i < 12; i++)
        {
            warehouse.AddStockInOrOutRecord(ELocationType.E_Enter, rm.Next(0, 1000), rm.Next(0, 100), i);
            warehouse.AddStockInOrOutRecord(ELocationType.E_Exit, rm.Next(0, 1000), rm.Next(0, 100), i);
        }
    }

	#endregion

	#region SortingLine

    public static void UpdateCurrentEnergyConsumptionTest()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();

        List<int> listAllSortlineID = warehouse.GetAllSortingLineID();
        foreach(var item in listAllSortlineID)
        {
            FSortingLine sortlineData = warehouse.GetLocationDataWithID<FSortingLine>(item);
            sortlineData.CurrentEnergyConsumption = rm.Next(0, 100);
        }
        
    }

    public static void UpdateRecordEnergyConsumptionTest()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();

        List<int> listAllSortlineID = warehouse.GetAllSortingLineID();
        foreach (var item in listAllSortlineID)
        {
            FSortingLine sortlineData = warehouse.GetLocationDataWithID<FSortingLine>(item);
            for (int i = 0; i < 14; i++)
            {
                sortlineData.AddEnergyConsumptionRecord(rm.Next(0, 100),TimeUtil.GetRandomTimestamp());
            }
            sortlineData.UpdatePullDataTime(JsonFildName.SortingLine.SortingLineDataRefreshTime, TimeUtil.GetTimestamp());
        }
    }

	public static void UpdateCurrentSortingLineDataTest()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();

        List<int> listAllSortlineID = warehouse.GetAllSortingLineID();
        foreach (var item in listAllSortlineID)
        {
            FSortingLine sortlineData = warehouse.GetLocationDataWithID<FSortingLine>(item);
            int iCompleted = rm.Next(0, 100);
            int iPlan = iCompleted + 10;
            double dDuration = rm.Next(1, 24);
            double dEfficient = (double)iCompleted / dDuration;
            sortlineData.CurrentSortingData.UpdateCurrentSortingData(iCompleted , iPlan , dDuration , dEfficient);
        }
    }

    public static void UpdateRecordSortingLineDataTest()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();

        List<int> listAllSortlineID = warehouse.GetAllSortingLineID();
        foreach (var item in listAllSortlineID)
        {
            FSortingLine sortlineData = warehouse.GetLocationDataWithID<FSortingLine>(item);
            for (int i = 0; i < 14; i++)
            {
                int iCompleted = rm.Next(0, 100);
                double dDuration = rm.Next(1, 24);
                double dEfficient = (double)iCompleted / dDuration;

                sortlineData.AddSortingCountRecord(iCompleted, 0, TimeUtil.GetRandomTimestamp());
                sortlineData.AddSortingDurationRecord(dDuration , TimeUtil.GetRandomTimestamp());
                sortlineData.AddSortingEfficientRecord(dEfficient,  TimeUtil.GetRandomTimestamp());
            }
            sortlineData.UpdatePullDataTime(JsonFildName.SortingLine.SortingLineDataRefreshTime, TimeUtil.GetTimestamp());
        }
    }

    public static void UpdateCurrentSortingCheckTest()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();

        List<int> listAllSortlineID = warehouse.GetAllSortingLineID();
        foreach (var item in listAllSortlineID)
        {
            FSortingLine sortlineData = warehouse.GetLocationDataWithID<FSortingLine>(item);
            int iCompleted = rm.Next(0, 100);
            int iPlan = iCompleted + 10;
            sortlineData.CurrentSortingCheckInCompleted = iCompleted;
            sortlineData.CurrentSortingCheckInPlan = iPlan;
            sortlineData.UpdatePullDataTime(JsonFildName.SortingLine.CheckRefreshTime, TimeUtil.GetTimestamp());
        }
    }

    public static void UpdateRecordSortingCheckTest()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();

        List<int> listAllSortlineID = warehouse.GetAllSortingLineID();
        foreach (var item in listAllSortlineID)
        {
            FSortingLine sortlineData = warehouse.GetLocationDataWithID<FSortingLine>(item);
            int iCompleted = rm.Next(0, 100);
            int iPlan = iCompleted + 10;
            sortlineData.AddCheckRecordRecord(iPlan, iCompleted, TimeUtil.GetTimestamp());
            sortlineData.UpdatePullDataTime(JsonFildName.SortingLine.CheckRefreshTime, TimeUtil.GetTimestamp());
        }
    }

	#endregion

	#region OverView
	public static void UpdateVisitorCurrentDataTest()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();
        FOverView overViewData = warehouse.GetLocationDataWithID<FOverView>(1);
        int iGuestNum = rm.Next(0, 100);
        int iStaffNum = rm.Next(0, 100);
        int iCarNum = rm.Next(0, 100);

        overViewData.CurrentVisitor.UpdateVisitorType(FCurrentVisitor.EVisitorType.E_Car, iCarNum);
        overViewData.CurrentVisitor.UpdateVisitorType(FCurrentVisitor.EVisitorType.E_Guest, iGuestNum);
        overViewData.CurrentVisitor.UpdateVisitorType(FCurrentVisitor.EVisitorType.E_Staff, iStaffNum);
        overViewData.CurrentVisitor.UpdateVisitorType(FCurrentVisitor.EVisitorType.E_Total, iCarNum + iGuestNum + iStaffNum);
        overViewData.UpdatePullDataTime(JsonFildName.OverView.VisitorRefreshTime, TimeUtil.GetTimestamp());
    }

    public static void UpdateVisitorRecordData()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();
        FOverView overViewData = warehouse.GetLocationDataWithID<FOverView>(1);

        for (int i = 0; i < 14; i++)
        {
            overViewData.AddVisitorRecordData(rm.Next(0, 1000), TimeUtil.GetRandomTimestamp());
        }
        overViewData.UpdatePullDataTime(JsonFildName.OverView.VisitorRefreshTime, TimeUtil.GetTimestamp());
    }

    public static void UpdateAirPressureCurrentDataTest()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();
        FOverView overViewData = warehouse.GetLocationDataWithID<FOverView>(1);
        overViewData.CurrentAirPressure = rm.Next(0, 1000);
        overViewData.UpdatePullDataTime(JsonFildName.OverView.AirPressureRefreshTime, TimeUtil.GetTimestamp());
    }

    public static void UpdateAirPressureRecordData()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();
        FOverView overViewData = warehouse.GetLocationDataWithID<FOverView>(1);

        for (int i = 0; i < 12; i++)
        {
            overViewData.AddAirPressureRecordData(rm.Next(0, 1000), TimeUtil.GetRandomTimestamp());
        }
        overViewData.UpdatePullDataTime(JsonFildName.OverView.AirPressureRefreshTime, TimeUtil.GetTimestamp());
    }

    public static void UpdateVoltageCurrentDataTest()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();
        FOverView overViewData = warehouse.GetLocationDataWithID<FOverView>(1);
        overViewData.CurrentVoltage = rm.Next(0, 1000);
        overViewData.UpdatePullDataTime(JsonFildName.OverView.VoltageRefreshTime, TimeUtil.GetTimestamp());
    }

    public static void UpdateVoltageRecordData()
    {
        System.Random rm = new System.Random();
        FWarehouse warehouse = FWarehouse.GetInstance();
        FOverView overViewData = warehouse.GetLocationDataWithID<FOverView>(1);

        for (int i = 0; i < 12; i++)
        {
            overViewData.AddVoltageRecordData(rm.Next(0, 1000), TimeUtil.GetRandomTimestamp());
        }
        overViewData.UpdatePullDataTime(JsonFildName.OverView.VoltageRefreshTime, TimeUtil.GetTimestamp());
    }
	#endregion

	#endregion

}

public struct FWarehouseHumitureRecord
{
    public double Temperature { get; set; }
    public double Humidity { get; set; }
    public long Timestamp { get; set; }
}

public struct FWarehouseRepertoryRecord
{
    public int RepertoryInBuff { get; set; }
    public int RepertoryInStore { get; set; }
    public long Timestamp { get; set; }

}

public struct FWarehouseStockInOrOutRecord
{
    public int Average { get; set; }
    public double Efficient { get; set; }
    public long Timestamp { get; set; }
}

public class FLocation
{
    public object Data { get; set; }
    public FLocation(int iID)
    {
        ID = iID;
    }
    public int ID { get; set; }
    public string CNName { get; set; }
    public string ENName { get; set; }

    private string m_strCode;
    public string Code {
        get
        {
            return m_strCode;
        }
        set 
        {
            m_strCode = value;
        } 
    }
    public int GroupID { get; set; }

    public EWarningLevel WarningLevel { get; set; }

    public ELocationType LocationType { get; set; }

    public ELocationType BigLocationType { get; set; }


    public void SetLocationType(ELocationType iType)
    {
        BigLocationType = iType;
        if (ToolUtils.LocationCheck(iType, ELocationType.E_EnterOrExit))
        {
            BigLocationType = ELocationType.E_EnterOrExit;
            Data = new FEnterOrExitData();
        }
        else if (ToolUtils.LocationCheck(iType, ELocationType.E_SortingLine))
        {
            BigLocationType = ELocationType.E_SortingLine;
            Data = new FSortingLine(ID);
        }
        else if (ToolUtils.LocationCheck(iType, ELocationType.E_Exterior))
        {
            Data = new FOverView();
        }
        else if (ToolUtils.LocationCheck(iType, ELocationType.E_Interior))
        {
            Data = new FExterior();
            
        }
        LocationType = iType;
    }
}

public class FEnterOrExitData : DataBase
{
    public double CurrentTemperature { get; set; }
    public double CurrentHumidity { get; set; }
    public int CurrentRepertoryInBuff { get; set; }
    public int CurrentRepertoryInStore { get; set; }
    
    public long Time { get; set; }

    //当前出入库数量和效率
    public int CurrentInOrOutStock { get; set; }
    public double CurrentInOrOutStockEfficient { get; set; }

    private List<FWarehouseRepertoryRecord> m_listRepertoryRecord = new List<FWarehouseRepertoryRecord>();
    private List<FWarehouseHumitureRecord> m_listHumitureRecord = new List<FWarehouseHumitureRecord>();
    private List<FWarehouseStockInOrOutRecord> m_listStockInOrOutRecord = new List<FWarehouseStockInOrOutRecord>();

    #region Humiture
    public List<FWarehouseHumitureRecord> GetNearestHumiture(int iCount = 1)
    {
        List<FWarehouseHumitureRecord> dataSource = m_listHumitureRecord;
        List<FWarehouseHumitureRecord> listResult = new List<FWarehouseHumitureRecord>();

        int iAlreadyAddCount = 0;
        for (int i = dataSource.Count - 1; i >= 0; i--)
        {
            listResult.Add(dataSource[i]);
            iAlreadyAddCount++;
            if (iAlreadyAddCount >= iCount)
            {
                break;
            }
        }
        return listResult;
    }

    public void AddHumitureRecord(double dTemperature, double dHumidity, long lTimestamp)
    {
        List<FWarehouseHumitureRecord> dataDestination = m_listHumitureRecord;
        FWarehouseHumitureRecord humiture = new FWarehouseHumitureRecord();
        humiture.Temperature = dTemperature;
        humiture.Humidity = dHumidity;
        humiture.Timestamp = lTimestamp;

        dataDestination.Add(humiture);
    }
    #endregion

    #region Repertory
    public void AddRepertoryRecord(int iRepertoryInBuff, int iRepertoryInStore , long lTimestamp)
    {
        
        FWarehouseRepertoryRecord repertory = new FWarehouseRepertoryRecord()
        {
            RepertoryInBuff = iRepertoryInBuff,
            RepertoryInStore = iRepertoryInStore,
            Timestamp = lTimestamp
        };
        m_listRepertoryRecord.Add(repertory);
    }

    public List<FWarehouseRepertoryRecord> GetNearestRepertoryRecord(int iCount)
    {
        return ToolUtils.GetCountFromList(m_listRepertoryRecord, iCount);
    }

    public void AddStockInOrOutRecord(int iAverage, double Efficient , long lTimestamp)
    {
        FWarehouseStockInOrOutRecord repertory = new FWarehouseStockInOrOutRecord()
        {
            Average = iAverage,
            Efficient = Efficient,
            Timestamp = lTimestamp
        };
        m_listStockInOrOutRecord.Add(repertory);
    }
    public List<FWarehouseStockInOrOutRecord> GetNearestStockInOrOutRecord(int iCount)
    {
        return ToolUtils.GetCountFromList(m_listStockInOrOutRecord, iCount);
    }
    #endregion


    public override void InvalidDataCheck(string str)
    {
        switch (str)
        {
            case JsonFildName.Warehouse.Record_Temperature:
            case JsonFildName.Warehouse.Record_Humidity:
                ToolUtils.LimitListMaxCount<FWarehouseHumitureRecord>(m_listHumitureRecord, RecordCount.RecordHumiture_2);
                break;
            case JsonFildName.Warehouse.Record_Repertory:
                ToolUtils.LimitListMaxCount<FWarehouseRepertoryRecord>(m_listRepertoryRecord, RecordCount.RecordRepertory);
                break;
            case JsonFildName.Warehouse.Record_StockInOrOut:
                ToolUtils.LimitListMaxCount<FWarehouseStockInOrOutRecord>(m_listStockInOrOutRecord, RecordCount.RecordStockInOrOut);
                break;
            default:
                ToolUtils.LimitListMaxCount<FWarehouseHumitureRecord>(m_listHumitureRecord, RecordCount.RecordHumiture_2);
                ToolUtils.LimitListMaxCount<FWarehouseRepertoryRecord>(m_listRepertoryRecord, RecordCount.RecordRepertory);
                ToolUtils.LimitListMaxCount<FWarehouseStockInOrOutRecord>(m_listStockInOrOutRecord, RecordCount.RecordStockInOrOut);
                break;
        }
        base.InvalidDataCheck(str);
    }
}